package com.seqb.manage.paper.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.seqb.common.mybatis.WrapperHandler;
import com.seqb.manage.paper.dao.PaperDao;
import com.seqb.manage.paper.dao.PaperQuestionDao;
import com.seqb.manage.paper.domain.PaperDO;
import com.seqb.manage.paper.domain.PaperQuestionDO;
import com.seqb.manage.paper.service.PaperService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author lilinshen
 * @title 试卷表
 * @description 请填写相关描述
 * @date 2019-12-04
 */
@Service
public class PaperServiceImpl implements PaperService {
    @Autowired
    private PaperDao paperDao;
    @Autowired
    private PaperQuestionDao paperQuestionDao;

    @Override
    public PaperDO get(Long id) {
        return paperDao.selectById(id);
    }

    @Override
    public Page<PaperDO> list(Map<String, Object> map) {
        if (null != map && null != map.get("pageNumber") && null != map.get("pageSize")) {
            PageHelper.startPage(Integer.valueOf(map.get("pageNumber").toString()), Integer.valueOf(map.get("pageSize").toString()));
        } else {
            PageHelper.startPage(1, Integer.MAX_VALUE);
        }
        QueryWrapper queryWrapper = WrapperHandler.genQueryWrapper(map, PaperDO.class);
        return (Page<PaperDO>) paperDao.selectList(queryWrapper);
    }

    @Override
    public int count(Map<String, Object> map) {
        QueryWrapper queryWrapper = WrapperHandler.genQueryWrapper(map, PaperDO.class);
        return paperDao.selectCount(queryWrapper);
    }

    @Override
    @Transactional
    public int save(PaperDO paper) {
        return paperDao.insert(paper);
    }

    @Override
    @Transactional
    public int update(PaperDO paper) {
        return paperDao.updateById(paper);
    }

    @Override
    @Transactional
    public int remove(Long id) {
        return paperDao.deleteById(id);
    }

    @Override
    @Transactional
    public int batchRemove(Long[] ids) {
        return paperDao.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public String getPaperQuestion(Long id, Model model) {
        try {
            Map<String, Object> params = new HashMap<>();
            PaperDO paper = paperDao.selectById(id);
            PageHelper.startPage(1, Integer.MAX_VALUE);
            params.put("paperId", id);
            Page<PaperQuestionDO> page = paperQuestionDao.list(params);
            model.addAttribute("paper", paper);
            model.addAttribute("questions", page.getResult());
            return "manage/paper/paper.view";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
